/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.repository;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.SMS;

public interface SMSDao extends PagingAndSortingRepository<SMS, Long>, JpaSpecificationExecutor<SMS> {

	// 根据ip地址查询一天已接收了多少条短信
	@Query("select count(a.id) from SMS a where a.ip = ?1 and a.czrq between ?2 and ?3")
	int countByIp(String ip, Date begin, Date end);

	// 根据手机号查询一天已接收了多少条短信
	@Query("select count(a.id) from SMS a where a.mobile = ?1 and a.czrq between ?2 and ?3")
	int countByMobile(String mobile, Date begin, Date end);

	@Query("select a from SMS a where a.mobile = ?1 and a.czrq = ?2")
	Page<SMS> findByMobile(String mobile, Pageable pageRequest);
	
	@Query("select a from SMS a where a.mobile = ?1 and a.type = ?2")
	Page<SMS> findByMobileType(String mobile, int type, Pageable pageRequest);

	@Query("select a from SMS a where a.mobile = ?1 and a.captcha = ?2 and a.type = ?3")
	Page<SMS> findByCaptcha(String mobile, String captcha, int type, Pageable pageRequest);

}
